<?php
/**
 * Validator
 *
 * @author Andres Gutierrez <andres@phalconphp.com>
 * @author Eduar Carvajal <eduar@phalconphp.com>
 * @author Wenzel Pünter <wenzel@phelix.me>
 * @version 1.2.6
 * @package Phalcon
*/
namespace Phalcon\Mvc\Model;

use \Phalcon\Mvc\Model\Message;
use \Phalcon\Mvc\Model\Exception;

/**
 * Phalcon\Mvc\Model\Validator
 *
 * This is a base class for Phalcon\Mvc\Model validators
 *
 * @see https://github.com/phalcon/cphalcon/blob/1.2.6/ext/mvc/model/validator.c
 */
abstract class Validator
{
    /**
     * Options
     *
     * @var null|array
     * @access protected
    */
    protected $_options;

    /**
     * Messages
     *
     * @var null|array
     * @access protected
    */
    protected $_messages;

    /**
     * \Phalcon\Mvc\Model\Validator constructor
     *
     * @param array $options
     * @throws Exception
     */
    public function __construct($options)
    {
        if (is_array($options) === false) {
            throw new Exception('$options argument must be an Array');
        }

        $this->_options = $options;
    }

    /**
     * Appends a message to the validator
     *
     * @param string $message
     * @param string|null $field
     * @param string|null $type
     * @throws Exception
     */
    protected function appendMessage($message, $field = null, $type = null)
    {
        if (is_string($message) === false) {
            throw new Exception('Invalid parameter type.');
        }

        if (is_null($field) === false &&
            is_string($field) === false) {
            throw new Exception('Invalid parameter type.');
        }

        if (is_null($field) === true) {
            $type = str_replace('Validator', '', __CLASS__);
        } elseif (is_string($field) === false) {
            throw new Exception('Invalid parameter type.');
        }

        if (is_array($this->_messages) === false) {
            $this->_messages = array();
        }
        $this->_messages[] = new Message($message, $field, $type);
    }

    /**
     * Returns messages generated by the validator
     *
     * @return array|null
     */
    public function getMessages()
    {
        return $this->_messages;
    }

    /**
     * Returns all the options from the validator
     *
     * @return array|null
     */
    protected function getOptions()
    {
        return $this->_options;
    }

    /**
     * Returns an option
     *
     * @param string $option
     * @return mixed
     * @throws Exception
     */
    protected function getOption($option)
    {
        if (is_string($option) === false) {
            throw new Exception('Invalid parameter type.');
        }

        if (isset($this->_options[$option]) === true) {
            return $this->_options[$option];
        }

        return;
    }

    /**
     * Check whether a option has been defined in the validator options
     *
     * @param string $option
     * @return boolean
     * @throws Exception
     */
    protected function isSetOption($option)
    {
        if (is_string($option) === false) {
            throw new Exception('Invalid parameter type.');
        }

        return isset($this->_options[$option]);
    }
}
